import unittest
from autoframe.common.functions import open_browser
from autoframe.page.login_page import LoginPage
from autoframe.page.index_page import IndexPage
import os
from autoframe.common.data_operation import DataOperation
import ddt


project_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
path = os.path.join(project_path, 'data/registry.xlsx')
account_list1 = DataOperation(path, 'registry.xlsx', 2).read_data_return_dict()

@ddt.ddt
class LoginTestCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        # 打开浏览器
        driver = open_browser()
        # 创建了对象
        cls.login = LoginPage(driver)
        cls.index = IndexPage(driver)

    @ddt.data(*account_list1)
    def test_01_login(self,data):
        # 输入网址
        self.login.get('http://172.16.140.57/ecshop/')
        # 点击请登录
        self.login.click_login()
        # 输入账号
        self.login.input_username(data['user'])
        # 输入密码
        self.login.input_password(data['password'])
        # 点击记住登录信息按钮
        self.login.remember()
        # 点击登录
        self.login.btn_login()

        # 断言登录成功
        username = self.index.font_text()
        print(username)
        self.assertEqual(data['user'], username)

    @classmethod
    def tearDownClass(cls) -> None:
        # 关闭浏览器
        cls.login.quit()


if __name__ == '__main__':
    unittest.main(verbosity=2)
